package com.rondhuit.uima.yahoo;

/**
 * Copyright 2011 RONDHUIT Co.,Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Level;
import org.apache.uima.util.Logger;
import org.xml.sax.SAXException;

public class KeyphraseExtractAnnotator extends JCasAnnotator_ImplBase {

  private static String CONFIG_PARAM_NAME_APPID = "appid";
  private String appid;
  private SAXParser saxParser;
  private Logger logger;
  private static final String SERVICE_URL = "http://jlp.yahooapis.jp/KeyphraseService/V1/extract";

  public void process(JCas aJCas) throws AnalysisEngineProcessException {
    String docText = aJCas.getDocumentText();
    URLConnection connection = null;
    try{
      connection = getServiceURL(docText).openConnection();
      connection.setDoOutput(true);
      KeyphraseResultHandler saxHandler = new KeyphraseResultHandler(aJCas);
      saxParser.parse(connection.getInputStream(), saxHandler);
    }
    catch(IOException e){
      System.out.println(e);
      throw new AnalysisEngineProcessException(e);
    }
    catch (SAXException e) {
      System.out.println(e);
      throw new AnalysisEngineProcessException(e);
    }
  }
  
  private URL getServiceURL(String docText) throws MalformedURLException, UnsupportedEncodingException {
    return new URL(SERVICE_URL + "?appid=" + appid + "&sentence=" + URLEncoder.encode(docText, "UTF-8"));
  }
  
  public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);

    // initialize annotator logger
    this.logger = this.getContext().getLogger();

    // create SAX parser
    try {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      saxParser = factory.newSAXParser();
    } catch (ParserConfigurationException ex) {
      throw new ResourceInitializationException(ex);
    } catch (SAXException ex) {
      throw new ResourceInitializationException(ex);
    }

    // get configuration parameters
    // get appid
    appid = (String) this.getContext().getConfigParameterValue(CONFIG_PARAM_NAME_APPID);
    this.logger.log(Level.CONFIG, "Parameter \"{0}\" set to \"{1}\"", new Object[] {
        CONFIG_PARAM_NAME_APPID, appid });
  }
}
